package cn.iocoder.yudao.module.member.controller.admin.memberconf;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.member.api.config.dto.MemberConfRespDTO;
import cn.iocoder.yudao.module.member.controller.admin.memberconf.vo.MemberConfPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.memberconf.vo.MemberConfRespVO;
import cn.iocoder.yudao.module.member.controller.admin.memberconf.vo.MemberConfSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.memberconf.MemberConfDO;
import cn.iocoder.yudao.module.member.dal.dataobject.memberconf.confmaterial.MemberConfMaterialDO;
import cn.iocoder.yudao.module.member.service.memberconf.MemberConfService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 会员主配置")
@RestController
@RequestMapping("/member/conf")
@Validated
public class MemberConfController {

    @Resource
    private MemberConfService confService;

    @PostMapping("/create")
    @Operation(summary = "创建会员主配置")
    @PreAuthorize("@ss.hasPermission('member:conf:create')")
    public CommonResult<Long> createConf(@Valid @RequestBody MemberConfSaveReqVO createReqVO) {
        return success(confService.createConf(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新会员主配置")
    @PreAuthorize("@ss.hasPermission('member:conf:update')")
    public CommonResult<Boolean> updateConf(@Valid @RequestBody MemberConfSaveReqVO updateReqVO) {
        confService.updateConf(updateReqVO);
        return success(true);
    }

    @GetMapping("/get-default")
    @Operation(summary = "获取默认配置")
    public CommonResult<MemberConfRespDTO> getDefaultConf() {
        return success(confService.getMemberConf(1L));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除会员主配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:conf:delete')")
    public CommonResult<Boolean> deleteConf(@RequestParam("id") Long id) {
        confService.deleteConf(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得会员主配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:conf:query')")
    public CommonResult<MemberConfRespVO> getConf(@RequestParam("id") Long id) {
        MemberConfDO conf = confService.getConf(id);
        return success(BeanUtils.toBean(conf, MemberConfRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得会员主配置分页")
    @PreAuthorize("@ss.hasPermission('member:conf:query')")
    public CommonResult<PageResult<MemberConfRespVO>> getConfPage(@Valid MemberConfPageReqVO pageReqVO) {
        PageResult<MemberConfDO> pageResult = confService.getConfPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MemberConfRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出会员主配置 Excel")
    @PreAuthorize("@ss.hasPermission('member:conf:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportConfExcel(@Valid MemberConfPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MemberConfDO> list = confService.getConfPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "会员主配置.xls", "数据", MemberConfRespVO.class,
                        BeanUtils.toBean(list, MemberConfRespVO.class));
    }

    // ==================== 子表（物料策略配置） ====================

    @GetMapping("/conf-material/list-by-member-conf-id")
    @Operation(summary = "获得物料策略配置列表")
    @Parameter(name = "memberConfId", description = "会员配置id")
    @PreAuthorize("@ss.hasPermission('member:conf:query')")
    public CommonResult<List<MemberConfMaterialDO>> getMemberConfMaterialListByMemberConfId(@RequestParam("memberConfId") Long memberConfId) {
        return success(confService.getConfMaterialListByMemberConfId(memberConfId));
    }

}